﻿using MengLin.OOP.DesignPattern.Interface;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace MengLin.OOP.DesignPattern.SimpleFactoryAdvanced
{
    /// <summary>
    /// 封装转移
    /// 把对象的创建交给第三方创建
    /// 简单工厂的最高级形态：工厂+配置+反射(通过技术手段消除依赖)
    /// </summary>
    public class ObjFactory
    {
        private static readonly string db = ConfigurationManager.AppSettings["db"];
        private static readonly string assemblyName = $"MengLin.OOP.DesignPattern.Implement.{db}";

        public static IUserService CreateInstance()
        {
            string className = $"{assemblyName}.{db}UserService";
            IUserService result = (IUserService)Assembly.Load(assemblyName).CreateInstance(className);

            return result;
        }
    }
}
